/**
 * 
 * 开场动画照片
 * 照片会像上移动, 每移动一定的距离会停顿一下
 * 移动的时候会出现打印机的声音
 * 
 */
class Photo extends egret.DisplayObjectContainer {
    /** 加载照片资源 */
    private photo_png: egret.Bitmap = Utils.createBitmapByName("photo_png");
    /** 照片移动速度 */
    public speed: number = 5;
    /** 照片的移动状态 */
    public isMove: boolean = false;
    /** 一张照片的大小 */
    public photoSize: number[] = [550, 1100, 1650, 2200];
    /** 零时记录照片吐出距离 */
    private bufferDistance: number = 0;
    /** 显示图片 */
    private nowShow = 0;
    /** 加载按键音资源 */
    public photo_sound: egret.Sound = RES.getRes("printPhoto_mp3");
    public channel = null;

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.init, this);
    }

    private init() {
        /** 将照片绘制在容器中 */
        this.addChild(this.photo_png);
        this.x = this.stage.stageWidth * .15 + 3;
        this.y = 741;

        for (let i = 1; i < 5; i++) {
            const png = Utils.createBitmapByName(`photo_${i}_png`);
            this.addChild(png);
            png.x = (this.width - 10 - png.width) / 2;
            const a = this.photoSize[i - 2] || 0;
            png.y = a + (i * 30);
            png.name = `${i}`;
            png.alpha = 0;
        }
        this.addEventListener(egret.Event.ENTER_FRAME, this.photoMove, this);
    }

    private photoMove() {
        if (!this.isMove) return;
        if (this.photoSize.length == 0) return this.photoOver();
        if (this.channel == null) {
            this.channel = this.photo_sound.play(.8, -1);
            this.channel.volume = .5;
        }
        /** 移动照片 */
        this.y -= this.speed;
        /** 存储照片移动距离 */
        this.bufferDistance += this.speed;
        /** 吐完一张 */
        if (this.bufferDistance > this.photoSize[0]) this.photoPause();
    }
    /** 照片停顿 */
    private photoPause() {
        this.isMove = false;
        this.photoSize.shift();
        this.channel.stop();
        this.channel = null;

        this.nowShow += 1;
        const png = this.getChildByName(this.nowShow.toString());
        egret.Tween.get(png).to({ alpha: 1 }, 800);

        this.dispatchEvent(new egret.Event("one_wait"));
    }
    /** 完结 */
    private photoOver() {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.photoMove, this);
        this.dispatchEvent(new egret.Event("photo_over"));
    }
}